﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Zephyr
{
    /// <summary>
    /// Login control to authenticate Zephyr users
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();

            _countFailures = 0;
            _dicLogins = new Dictionary<string, string>() { { "qwood", "tbwells" }, { "beichhorn", "everest" } };
        }

        const int MAX_FAILURES = 3;
        int _countFailures;
        Dictionary<string, string> _dicLogins;

        public string Username { get { return _txtUserName.Text; } }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }

        private void ButtonLogin_Click(object sender, RoutedEventArgs e)
        {
            string user = _txtUserName.Text.ToLower();
            
            if (_dicLogins.ContainsKey(user)
                && _dicLogins[user] == _txtPassword.Password)
            {
                DialogResult = true;
            }
            else
            {                
                if (++_countFailures >= MAX_FAILURES)
                {
                    DialogResult = false;
                    this.Close();
                }
                else
                {
                    _labelFailed.Content = "Failed attempt " + _countFailures;
                    _txtUserName.Clear();
                    _txtPassword.Clear();
                    _txtUserName.Focus();
                }

            }
        }
    }
}
